package com.food.restaurantpro.view;

import java.util.Observable;
import java.util.Observer;

import com.food.restaurantpro.R;
import com.food.restaurantpro.activity.MainActivity;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.connection.IServerService.Methods;
import com.food.restaurantpro.database.tables.ReservationTable;
import com.food.restaurantpro.objects.Reservation;
import com.food.restaurantpro.utils.AlertDialogInterface;
import com.food.restaurantpro.utils.AlertDialogManager;
import com.food.restaurantpro.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class ReservationItemView extends RelativeLayout implements OnClickListener, AlertDialogInterface {
	private Context mContext;
	private Reservation mReservation;
	private AlertDialogInterface mCallback;

	public ReservationItemView(Context context, Reservation reservation, AlertDialogInterface callback) {
		super(context);
		mContext = context;
		mCallback = callback;
		setReservationInfo(reservation);
		LayoutInflater li = (LayoutInflater) this.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		li.inflate(R.layout.item_reservation, this, true);
		setupUI();
	}

	public void setupUI() {
		findViewById(R.id.btnExpandEditReservation).setOnClickListener(this);
		findViewById(R.id.btnExpandCancelReservation).setOnClickListener(this);
		findViewById(R.id.btnExpandShareReservation).setOnClickListener(this);
		((TextView)findViewById(R.id.tvDateExpandItemReservation)).setText(mReservation.getReservationDateWithEE());
		((TextView)findViewById(R.id.tvTimeExpandItemReservation)).setText(mReservation.getReservationTimeWithPM());
		((TextView)findViewById(R.id.tvPeopleExpandItemReservation)).setText(mReservation.getReservationNumberWithText());
	}
	
	public Reservation getReservationInfo() {
		return mReservation;
	}

	public void setReservationInfo(Reservation mReservation) {
		this.mReservation = mReservation;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnExpandEditReservation:
			AlertDialogManager.showCustomDialog((Activity) mContext, new AlertDialogInterface() {
				
				@Override
				public void onChooseYesCallback() {
					// TODO Auto-generated method stub
					((MainActivity)mContext).showEditReservationFragment(mReservation);
				}
				
				@Override
				public void onChooseNoCallback() {
					// TODO Auto-generated method stub
					
				}
			},
				mContext.getString(R.string.cancel_reservation_title), 
				mContext.getString(R.string.cancel_reservation_message),
				mContext.getString(R.string.yes),
				mContext.getString(R.string.no), true);
			break;
		case R.id.btnExpandCancelReservation:
			AlertDialogManager.showCustomDialog((Activity) mContext, this,
					mContext.getString(R.string.cancel_reservation_title), 
					mContext.getString(R.string.cancel_reservation_message),
					mContext.getString(R.string.yes),
					mContext.getString(R.string.no), true);
			break;
		case R.id.btnExpandShareReservation:
			
			break;

		default:
			break;
		}
	}

	@Override
	public void onChooseYesCallback() {
		// TODO Auto-generated method stub
		mReservation.setReservationStatus("0");
		DataHelper.getInstance().addObserver(new Observer() {
			@Override
			public void update(Observable observable, Object data) {
				// TODO Auto-generated method stub
				DataResult result = (DataResult) data;
				if (result.getMethodName().equals(Methods.closeReservation)) {
					((MainActivity)mContext).hideProcessDialog();
					String status = (String) result.getData();
					if(!Utils.isEmpty(status) && status.equals("true")) {
						Utils.showToast("Close succesfull");
						ReservationTable.updateReservationByID(mReservation);
						if(mCallback != null) {
							mCallback.onChooseYesCallback();
						}
					} else {
						Utils.showToast("Close fail");
						mReservation.setReservationStatus("1");
					}
					DataHelper.getInstance().deleteObserver(this);
				}
			}
		});
		((MainActivity)mContext).showProcessDialog();
		DataHelper.getInstance().closeReservation(mReservation);
	}

	@Override
	public void onChooseNoCallback() {
		// TODO Auto-generated method stub
		
	}
}
